package com.jsp.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsp.bean.DriverBean;
import com.jsp.service.DriverService;

public class DriverServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private DriverBean driverBean=null;
	private DriverService driverService=null;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
        this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
		
		response.setCharacterEncoding("UTF-8");
		String mark = request.getParameter("mark");
		if("insert".equals(mark)){
			try {
				insert(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("delete".equals(mark)){
			try {
				delete(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else if("update".equals(mark)){
			try {
				update(request,response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		
	

}

	private void insert(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		String name_dr=request.getParameter("name_dr");
		String age_dr=request.getParameter("age_dr");
		String tel_dr=request.getParameter("tel_dr");
		String address_dr=request.getParameter("address_dr");
		String idcard_dr=request.getParameter("idcard_dr");
		String state_dr=request.getParameter("state_dr");
		driverService=new DriverService();
		driverBean=driverService.inesrt(name_dr, age_dr, tel_dr, address_dr, idcard_dr, state_dr);
		if(null!=driverBean){			
			request.getRequestDispatcher("/admin/insertdriversuccess.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/admin/insertdrivererror.jsp").forward(request, response);
		}
		
	}

	private void update(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		String id=request.getParameter("id");
		String name_dr=request.getParameter("name_dr");
		String age_dr=request.getParameter("age_dr");
		String tel_dr=request.getParameter("tel_dr");
		String address_dr=request.getParameter("address_dr");
		String idcard_dr=request.getParameter("idcard_dr");
		String state_dr=request.getParameter("state_dr");
		
		driverService=new DriverService();
		driverBean=driverService.update(id,name_dr, age_dr, tel_dr, address_dr, idcard_dr,state_dr);
		if(null!=driverBean){
			
			request.getRequestDispatcher("/admin/updatedriver.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/admin/updatedriver.jsp").forward(request, response);
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response)throws SQLException, ServletException, IOException {
		// TODO Auto-generated method stub
		String id=request.getParameter("id");
		String name_dr=request.getParameter("name_dr");
		String age_dr=request.getParameter("age_dr");
		String tel_dr=request.getParameter("tel_dr");
		String address_dr=request.getParameter("address_dr");
		String idcard_dr=request.getParameter("idcard_dr");
		String state_dr=request.getParameter("state_dr");
		System.out.println("ss"+id);
		driverService=new DriverService();
		driverBean=driverService.delete(id,name_dr, age_dr, tel_dr, address_dr, idcard_dr, state_dr);
		if(null!=driverBean){
			
			request.getRequestDispatcher("/admin/deletedriversuccess.jsp").forward(request, response);
		}else{
			request.getRequestDispatcher("/admin/deletedrivererror.jsp").forward(request, response);
		}
	}
}
